﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ElectronicWMSA.Comm.ClassFiles;
using SYDZ.WMS.DBUtility;
using WMSABL;
using ElectronicWMSA.Comm.Forms.Message;

namespace ElectronicWMSA.Warehouse
{
    public partial class Frm_PROAV : Form
    {
        private string strWhere1 = "";
        private string strWhere2 = "";
        PublicDataSet bll_PublicDataSet = new PublicDataSet();
        public Frm_PROAV()
        {
            InitializeComponent();
            this.pageControl_Stock.PageCurrent = 1;
            this.pageControl_Stock.PageSize = 50;

            this.pageControl_Out.PageCurrent = 1;
            this.pageControl_Out.PageSize = 50;
        }

        #region DBindData
        private int DBindData1()
        {
            Comm.Forms.MsControls.PageData pageData = new Comm.Forms.MsControls.PageData();
            pageData.TableName = "PROAV_TB";
            pageData.PrimaryKey = "NID";
            pageData.OrderStr = "NID";
            pageData.PageIndex = this.pageControl_Stock.PageCurrent;
            pageData.PageSize = this.pageControl_Stock.PageSize;
            pageData.QueryCondition = strWhere1;
            pageData.QueryFieldName = "*";

            this.pageControl_Stock.bindingSource.DataSource = pageData.QueryDataTable().Tables[0];
            this.pageControl_Stock.bindingNavigator.BindingSource = pageControl_Stock.bindingSource;
            this.dataGridView_Stock.AutoGenerateColumns = false;
            this.dataGridView_Stock.DataSource = this.pageControl_Stock.bindingSource;
            this.dataGridView_Stock.ClearSelection();
            this.dataGridView_Stock.CurrentCell = null;
            return pageData.TotalCount;
        }
        private int DBindData2()
        {
            Comm.Forms.MsControls.PageData pageData = new Comm.Forms.MsControls.PageData();
            pageData.TableName = "PROAV_TB";
            pageData.PrimaryKey = "NID";
            pageData.OrderStr = "NID";
            pageData.PageIndex = this.pageControl_Out.PageCurrent;
            pageData.PageSize = this.pageControl_Out.PageSize;
            pageData.QueryCondition = strWhere2;
            pageData.QueryFieldName = "*";

            this.pageControl_Out.bindingSource.DataSource = pageData.QueryDataTable().Tables[0];
            this.pageControl_Out.bindingNavigator.BindingSource = pageControl_Out.bindingSource;
            this.dataGridView_Out.AutoGenerateColumns = false;
            this.dataGridView_Out.DataSource = this.pageControl_Out.bindingSource;
            this.dataGridView_Out.ClearSelection();
            this.dataGridView_Out.CurrentCell = null;
            return pageData.TotalCount;
        }
        private int pageControl2_EventPaging(Comm.Forms.MsControls.EventPagingArg e)
        {
            return DBindData1();
        }
        private int pageControl_Out_EventPaging(Comm.Forms.MsControls.EventPagingArg e)
        {
            return DBindData2();
        }

        private void dataGridView_Out_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            SolidBrush b = new SolidBrush(dataGridView_Out.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), dataGridView_Out.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
        }
        private void dataGridView2_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            SolidBrush b = new SolidBrush(dataGridView_Stock.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), dataGridView_Stock.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
        }
        #endregion

        #region 查询、删除、导出
        private void btnSearch2_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                strWhere1 = " DelFalg='0' AND OutList_NO IS NULL AND OUT_Data IS NULL  AND OUT_Where IS NULL  ";
            }
            else
            {
                strWhere1 = " DelFalg='1' AND OutList_NO IS NULL AND OUT_Data IS NULL  AND OUT_Where IS NULL  ";
            }
            if (!string.IsNullOrEmpty(txt_PONumRu.Text.Trim()))
            {
                this.strWhere1 += "   AND INList_NO like '%" + txt_PONumRu.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(txt_MachineRu.Text))
            {
                this.strWhere1 += "   AND Product_Model  like '%" + txt_MachineRu.Text.Trim() + "'";
            }
            if (!string.IsNullOrEmpty(txt_SNORu.Text))
            {
                this.strWhere1 += "   AND Serial_Number like '%" + txt_SNORu.Text.Trim() + "%'";
            }
            this.pageControl_Stock.Bind();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            DataTable dt;
            string SelectSQL = "SELECT Product_Model AS '机型',Serial_Number AS '序列号', INList_NO AS '入库单号',IN_Time AS '入库时间'  FROM PROAV_TB  ";
            if (strWhere1 != "")
            {
                SelectSQL += "WHERE  " + strWhere1 + "    ";
            }

            dt = DbHelperSQL.ExecuteDataTable(SelectSQL);
            ExPut.GetNopiExcel(dt);
        }
        #endregion

        #region 查询、导出
        private void btn_OutSearch_Click(object sender, EventArgs e)
        {
            strWhere2 = " DelFalg='1' AND OutList_NO!='' AND OUT_Data!='' AND OUT_Where!='' ";
            if (!string.IsNullOrEmpty(txt_PoNum_out.Text.Trim()))
            {
                this.strWhere2 += "   AND OutList_NO like '%" + txt_PoNum_out.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(txt_OutData.Text.Trim()))
            {
                this.strWhere2 += "   AND OUT_Data like '%" + txt_OutData.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(txt_Where.Text.Trim()))
            {
                this.strWhere2 += "   AND OUT_Where like '%" + txt_Where.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(txt_Ma_out.Text))
            {
                this.strWhere2 += "   AND   Product_Model  like '%" + txt_Ma_out.Text.Trim() + "'";
            }
            if (!string.IsNullOrEmpty(txt_SNO_Out.Text))
            {
                this.strWhere2 += "   AND Serial_Number like '%" + txt_SNO_Out.Text.Trim() + "%'";
            }
            this.pageControl_Out.Bind();
        }

        private void btnImport_Out_Click(object sender, EventArgs e)
        {
            DataTable dt;
            string SelectSQL = "SELECT Product_Model AS '机型',Serial_Number AS '序列号', INList_NO AS '入库单号',IN_Time AS '入库时间', OutList_NO AS '出库单号',OUT_Data AS '出库日期',OUT_Time AS '扫描时间',OUT_Where AS '仕向地',Update_User AS '操作人'  FROM PROAV_TB   ";
            if (strWhere2 != "")
            {
                SelectSQL += "WHERE  " + strWhere2 + "    ";
            }

            dt = DbHelperSQL.ExecuteDataTable(SelectSQL);
            ExPut.GetNopiExcel(dt);
        }
        #endregion

        private int UpdateID = 0;
        private void dataGridView_Stock_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            UpdateID = 0;
            if (e.RowIndex > -1)
            {
                if (dataGridView_Stock.Rows.Count > 0)
                {
                    UpdateID = Convert.ToInt32(dataGridView_Stock.CurrentRow.Cells["NID"].Value.ToString());
                    dataGridView_Stock.Rows[e.RowIndex].Selected = true;
                }
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (UpdateID > 0)
            {
                if (MessageClass.enquire("确认删除数据？ ") == DialogResult.OK)
                {
                    string SelectSQL = " UPDATE PROAV_TB SET DelFalg=0 WHERE NID='" + UpdateID + "' ";
                    int result = DbHelperSQL.ExecuteSql(SelectSQL);
                    if (result > 0)
                    {
                        MessageClass.right("操作完成！");
                        dataGridView_Stock.ClearSelection();
                        UpdateID = 0;
                        this.pageControl_Stock.Bind();
                    }
                }
            }
            else
            {
                MessageClass.error("请选择数据！");
            }
        }

        private void btnReturn_Click(object sender, EventArgs e)
        {
            if (UpdateID2 > 0)
            {
                if (MessageClass.enquire("确认撤消出库？ ") == DialogResult.OK)
                {
                    string SelectSQL = " UPDATE PROAV_TB SET DelFalg=1,OutList_NO=NULL,OUT_Data=NULL,OUT_Time=NULL,OUT_Where=NULL WHERE NID='" + UpdateID2 + "' ";
                    int result = DbHelperSQL.ExecuteSql(SelectSQL);
                    if (result > 0)
                    {
                        MessageClass.right("操作完成！");
                        dataGridView_Out.ClearSelection();
                        this.pageControl_Out.Bind();
                        UpdateID2 = 0;
                        SystemVariable.AddLogInfo("PROVA出库", "用户:【" + SystemVariable.Login_Name + "】撤消了PROVA出库，数据编号为：【" + UpdateID2 + "】。");
                    }
                }
            }
            else
            {
                MessageClass.error("请选择数据！");
            }
        }
        private int UpdateID2 = 0;

        private void dataGridView_Out_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            UpdateID2 = 0;
            if (e.RowIndex > -1)
            {
                if (dataGridView_Out.Rows.Count > 0)
                {
                    UpdateID2 = Convert.ToInt32(dataGridView_Out.CurrentRow.Cells["NID2"].Value.ToString());
                    dataGridView_Out.Rows[e.RowIndex].Selected = true;
                }
            }
        }
    }
}
